package com.oldbook.manage.controller;

import com.oldbook.common.annotation.Log;
import com.oldbook.common.core.controller.BaseController;
import com.oldbook.common.core.domain.AjaxResult;
import com.oldbook.common.core.page.TableDataInfo;
import com.oldbook.common.enums.BusinessType;
import com.oldbook.common.utils.DateUtils;
import com.oldbook.common.utils.SecurityUtils;
import com.oldbook.common.utils.poi.ExcelUtil;
import com.oldbook.manage.domain.PublishInfo;
import com.oldbook.manage.service.IPublishInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 学生发布书籍信息Controller
 *
 * @author Exiaoke
 * @date 2025-02-18
 */
@RestController
@RequestMapping("/manage/publish")
@Api(tags = "学生发布书籍信息管理")
@Slf4j
public class PublishInfoController extends BaseController {

    @Autowired
    private IPublishInfoService publishInfoService;




/**
 * 查询学生发布书籍信息列表
 */
@PreAuthorize("@ss.hasPermi('manage:publish:list')")
@GetMapping("/list")
@ApiOperation("查询学生发布书籍信息列表")
    public TableDataInfo list(@ApiParam("学生发布书籍信息对象") PublishInfo publishInfo) {
        startPage();
        List<PublishInfo> list = publishInfoService.selectPublishInfoList(publishInfo);
        return getDataTable(list);
    }

    /**
     * 导出学生发布书籍信息列表
     */
    @PreAuthorize("@ss.hasPermi('manage:publish:export')")
    @Log(title = "学生发布书籍信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出学生发布书籍信息列表")
    public void export(HttpServletResponse response, @ApiParam("学生发布书籍信息对象") PublishInfo publishInfo) {
        List<PublishInfo> list = publishInfoService.selectPublishInfoList(publishInfo);
        ExcelUtil<PublishInfo> util = new ExcelUtil<PublishInfo>(PublishInfo.class);
        util.exportExcel(response, list, "学生发布书籍信息数据");
    }

    /**
     * 获取学生发布书籍信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:publish:query')")
    @GetMapping(value = "/{publishId}")
    @ApiOperation("获取学生发布书籍信息详细信息")
    public AjaxResult getInfo(@PathVariable("publishId") @ApiParam("publishId的值") Long publishId) {
        return success(publishInfoService.selectPublishInfoByPublishId(publishId));
    }

    /**
     * 新增学生发布书籍信息
     */
    @PreAuthorize("@ss.hasPermi('manage:publish:add')")
    @Log(title = "学生发布书籍信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增学生发布书籍信息")
    public AjaxResult add(@RequestBody @ApiParam("要添加的学生发布书籍信息对象") PublishInfo publishInfo) {
        publishInfo.setStudentId(SecurityUtils.getLoginUser().getUser().getStudentId());
        publishInfo.setCreateTime(DateUtils.getNowDate());
        publishInfo.setAuditBy("系统自动审核");
        publishInfo.setAuditTime(DateUtils.getNowDate());
        return toAjax(publishInfoService.insertPublishInfo(publishInfo));
    }

    /**
     * 修改学生发布书籍信息
     */
    @PreAuthorize("@ss.hasPermi('manage:publish:edit')")
    @Log(title = "学生发布书籍信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改学生发布书籍信息")
    public AjaxResult edit(@RequestBody @ApiParam("要修改的学生发布书籍信息对象") PublishInfo publishInfo) {
//
//        // 在add方法插入数据后添加：
//        PublishInfo newInfo = publishInfoService.selectPublishInfoByPublishId(publishInfo.getPublishId());
//        publishAuditService.autoAudit(newInfo, new AuditParam());
        return toAjax(publishInfoService.updatePublishInfo(publishInfo));
    }

    /**
     * 删除学生发布书籍信息
     */
    @PreAuthorize("@ss.hasPermi('manage:publish:remove')")
    @Log(title = "学生发布书籍信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{publishIds}")
    @ApiOperation("删除学生发布书籍信息")
    public AjaxResult remove(@PathVariable @ApiParam("要删除的publishId数组") Long[] publishIds) {
        return toAjax(publishInfoService.deletePublishInfoByPublishIds(publishIds));
    }


//    /**
//     * 自动审核发布信息
//     */
//    @PreAuthorize("@ss.hasPermi('manage:publish:audit')")
//    @Log(title = "学生发布书籍信息", businessType = BusinessType.OTHER)
//    @PostMapping("/audit/{publishId}")
//    @ApiOperation("自动审核发布信息")
//    public AjaxResult autoAudit(
//            @PathVariable @ApiParam("要审核的publishId") Long publishId,
//            @Validated @RequestBody @ApiParam("审核参数") AuditParam param) {
//
//        // 查询发布信息
//        PublishInfo info = publishInfoService.selectPublishInfoByPublishId(publishId);
//        if (info == null) {
//            return error("发布信息不存在");
//        }
//
//        // 检查审核参数是否为空
//        if (param == null) {
//            return error("审核参数不能为空");
//        }
//
//        // 记录审核日志
//        log.info("开始自动审核，publishId: {}, param: {}", publishId, param);
//
//        // 执行自动审核
//        try {
//            PublishAuditService.AuditResult result = publishAuditService.autoAudit(info, param);
//            return success(result);
//        } catch (Exception e) {
//            log.error("自动审核失败，publishId: {}, param: {}", publishId, param, e);
//            return error("自动审核失败：" + e.getMessage());
//        }
//    }

}